/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package io.demo;

import java.io.*;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author VANDH
 *
 */
// Viết ct lưu các khách sạn xuống file
// Đọc file và tìm khách sạn theo owner
// Bài tập Regular Expression -> kiểm tra dữ liệu nhập vô
// Các loại dữ liệu cần kiểm tra : họ tên, cmnd, ngày sinh, địa chỉ, điện thoại
public class StoreInBinary {

    public static void main(String[] args) throws IOException {
        String fileName = "D:\\binaryData.data";
        Scanner keyboard = new Scanner(System.in);
        int choice = 3;

        do {

            System.out.println("1. Write to file");
            System.out.println("2. Read from file");
            System.out.println("3. Exist");
            choice = keyboard.nextInt();

            switch (choice) {
                case 1:
                    writeToFile(fileName);
//                    writeToTextFile(fileName);
                    break;
                case 2:
                    readFromFile(fileName);
//                    readFromTextFile(fileName);
                    break;
            }
        } while (choice != 3);
    }

    private static void writeToFile(String fileName) throws IOException {
        DataOutputStream dos = null;
        try {
            dos = new DataOutputStream(new FileOutputStream(fileName));

            String studentName = "Nguyen Van A";
            int studentAge = 100;
            boolean female = false;

            dos.writeUTF(studentName);
            dos.writeInt(studentAge);
            dos.writeBoolean(female);

        } catch (FileNotFoundException ex) {
            Logger.getLogger(StoreInBinary.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (dos != null) {
                dos.close();
            }
        }
    }

    private static void readFromFile(String fileName) throws IOException {
        DataInputStream dis = null;

        try {
            dis = new DataInputStream(new FileInputStream(fileName));

            System.out.println(dis.readUTF());
            System.out.println(dis.readInt());
            System.out.println(dis.readBoolean());


        } catch (FileNotFoundException ex) {
            Logger.getLogger(StoreInBinary.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (dis != null) {
                dis.close();
            }
        }
    }

    private static void writeToTextFile(String fileName) throws IOException {
//        PrintWriter pw = null;
        BufferedWriter pw =
                new BufferedWriter(
                new OutputStreamWriter(
                new FileOutputStream(fileName), "UTF-8"));
        OutputStreamWriter t = new FileWriter(fileName);
        try {
//            pw = new PrintWriter(fileName);


            String studentName = "Nguyễn Văn Cừ";
            int studentAge = 100;
            boolean female = false;

//            pw.println(studentName);
//            pw.println(studentAge);
//            pw.println(female);
            pw.write(studentName);
            pw.newLine();
            pw.write(Integer.toString(studentAge));
            pw.newLine();
            pw.write(Boolean.toString(female));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(StoreInBinary.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            pw.close();
        }
    }

    private static void readFromTextFile(String fileName) throws IOException {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
            String line;

            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(StoreInBinary.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
